#include "main.h"
#include <math.h>

osThreadId_t task1_ID; // 任务1
osThreadId_t task2_ID; // 任务1
osThreadId_t task3_ID; // 任务1
osThreadId_t task4_ID;



ssapc_write_param_t *sle_uart_send_param = NULL;
#define DELAY_TIME_MS 1 // 延时时间，单位为毫秒




uint8_t uart_Send1[6]={0XAA,0X55,0X02,0X00,0X55,0XAA};//允许变道
uint8_t uart_Send2[6]={0XAA,0X55,0X01,0X00,0X55,0XAA};//不具备变道条件


uint8_t uart_Send4[6]={0xAA,0x55,0x03,0x00,0x55,0xAA};//解锁
uint8_t uart_Send5[6]={0xAA,0x55,0x04,0x00,0x55,0xAA};//上锁

uint8_t uart_Send6[10]={0xAA,0x55,0x05,0x00,0x01,0x00,0x00,0x00,0x55,0xAA};//解析的广播数据有车辆有右变道的趋势,AA 55 05 00 01 00 00 00 55 AA   0x01代表距离为1米
uint8_t uart_Send7[10]={0xAA,0x55,0x06,0x00,0x01,0x00,0x00,0x00,0x55,0xAA};//解析的广播数据有车辆有左变道的趋势



uint8_t uart_Send8[14]={0xAA,0X55,0X00,0X01,0X02,0X00,0X00,0X00,0X03,0X00,0X00,0X00,0X55,0XAA}; //右转获得发的雷达数据
uint8_t uart_Send9[14]={0xAA,0X55,0X01,0X01,0X02,0X00,0X00,0X00,0X03,0X00,0X00,0X00,0X55,0XAA}; //左转获得的雷达数据

uint8_t uart_Send10[10]={0xAA,0x55,0x07,0x00,0x01,0x00,0x00,0x00,0x55,0xAA};//解析的广播数据有超车趋势
uint8_t uart_Send11[10]={0xAA,0x55,0x08,0x00,0x01,0x00,0x00,0x00,0x55,0xAA};//解析的广播数据有车辆求救


void Client_Send(msg_data_t msg_data,uint16_t conn_ID)
{
    
    if (msg_data.value != NULL)
    {
       
        ssapc_write_param_t *sle_uart_send_param = &g_sle_send_param;
        sle_uart_send_param->data_len = msg_data.value_len;
        sle_uart_send_param->data = msg_data.value;
        ssapc_write_cmd(0, conn_ID, sle_uart_send_param);//数据写入服务端特征值,该函 数通过SSAP（StarLight Service Access Protocol）协议，将客户端数据写入到服务端指定的特征值句柄​（sle_uart_send_param->handle）。写入操作基于星闪的GATT-like服务模型，通过预发现的特征句柄实现端点定位

    }

}



static void Main_task(void)
{
    
    int a;
    
    uint16_t i;
    unused(i);
    unused(a);
    sle_client_init();
    uart1_init();
    uart2_init();
    
    while(1)
    {
        switch(voice_Cmd)
        {
          case 1:
          Client_Send(mag_Send_data1,0);//判断右向车道有无来车
          voice_Cmd=0;
          break;

          case 2:
          Client_Send(mag_Send_data2,0);//判断左向车道有无来车
          
          voice_Cmd=0;
          break;

          case 3:
          ec800_Call_Phone();//呼叫紧急联系人
          osDelay(200);
          voice_Cmd=0;
          break;

          case 4:
          Client_Send(mag_Send_data4,0);//打开双闪
          voice_Cmd=0;
          break;

          case 5:
          Client_Send(mag_Send_data5,0);//打开SOS投影灯
          voice_Cmd=0;
          break;
        }

        switch(flag)
        {
            case 1:
            uapi_uart_write(UART_BUS_1,uart_Send1,6,0xffffffff);
            flag=0;
            break;

            case 2:
            uapi_uart_write(UART_BUS_1,uart_Send2,6,0xffffffff);
            flag=0;
            break;


            case 3:
            printf("MPU6050");
            if(LOCK_state==1) //解锁状态下，检测到陀螺仪倾倒，说明发生事故了，拨通紧急联系人电话
            {
                ec800_Call_Phone();//呼叫紧急联系人
            }
            
            if(LOCK_state==0) //上锁状态下，检测到陀螺仪倾倒，说明车可能因为意外倾倒了，但人不在
            {
                printf("yiwai");
            }
            flag=0;
            break;


            case 4:
            uapi_uart_write(UART_BUS_1,uart_Send4,6,0xffffffff);
            flag=0;
            break;


            case 5:
            uapi_uart_write(UART_BUS_1,uart_Send5,6,0xffffffff);
            flag=0;
            break;

             case 6:
             uart_Send8[3]=abs(row);
             uart_Send8[4]=distance;
             uart_Send8[8]=speeed;
            uapi_uart_write(UART_BUS_1,uart_Send8,14,0xffffffff);
            flag=0;
            break;

            case 7:
            uart_Send9[3]=abs(row);
             uart_Send9[4]=distance;
             uart_Send9[8]=speeed;
            uapi_uart_write(UART_BUS_1,uart_Send9,14,0xffffffff);
            flag=0;
            break;

        }

          switch(Sle_Anounce_Flag)
        {
            case 1:
            uart_Send6[4]=(uint8_t)rssi_distance;
            printf("Z turn right\r\n");
            for(int i=0;i<10;i++)
            {
                printf("uart_Send6 %02x\r\n",uart_Send6[i]);
            }
            uapi_uart_write(UART_BUS_1,uart_Send6,10,0xffffffff);
            Sle_Anounce_Flag=0;
            break;

            case 2:
            uart_Send7[4]=(uint8_t)rssi_distance;
            printf("z turn left\r\n");
            uapi_uart_write(UART_BUS_1,uart_Send7,10,0xffffffff);
            Sle_Anounce_Flag=0;
            break;

            case 3:
            uart_Send10[4]=(uint8_t)rssi_distance;
            printf("overtake\r\n");
            uapi_uart_write(UART_BUS_1,uart_Send10,10,0xffffffff);
            Sle_Anounce_Flag=0;
            break;

            case 4:
            uart_Send11[4]=(uint8_t)rssi_distance;
            printf("sos/r/n");
            uapi_uart_write(UART_BUS_1,uart_Send11,10,0xffffffff);
            ec800_Call_Phone();//呼叫紧急联系人
            Sle_Anounce_Flag=0;
            break;



        }
        

        


        osDelay(DELAY_TIME_MS);
        
    }

}



static void kernel_task_example(void)
{

  
    osThreadAttr_t attr;
    attr.name = "Task1";              // 任务的名字
    attr.attr_bits = 0U;              // 属性位
    attr.cb_mem = NULL;               // 堆空间地址
    attr.cb_size = 0U;                // 堆空间大小
    attr.stack_mem = NULL;            // 栈空间地址
    attr.stack_size = 0x1000;         // 栈空间大小 单位:字节
    attr.priority = osPriorityNormal1; // 任务的优先级，数据越大，优先级越高

    task1_ID = osThreadNew((osThreadFunc_t)Main_task, NULL, &attr); // 创建任务1
    if (task1_ID != NULL) {
        printf("ID = %d, Create task1_ID is OK!\n", task1_ID);
    }


   
    

   

}

/* Run the sample. */
app_run(kernel_task_example);